package com.example.machine.controller;

import com.example.machine.model.*;
import com.example.machine.service.MachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-01-17
 * Time: 14:06
 */
@Slf4j
@RestController
@RequestMapping("/machine")
public class MachineController {
    //private  BookService bookService = new BookService();

    @Autowired
    private MachineService machineService;



    @RequestMapping("/getMachineListByPage")
    public Result getMachineListByPage(PageRequest pageRequest, HttpSession session){
        log.info("前端传递的翻页信息为",pageRequest);
        //用户登录校验
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo == null ||userInfo.getId() <= 0){
//            //用户未登录
//            return Result.unlogin();
//        }
        //对参数进行校验
        if(pageRequest.getPageSize()<0 || pageRequest.getPageSize()<1){
            return Result.fail("参数校验失败");
        }
        PageResult<MachineInfo> machineInfoPageResult = null;
        try{
            machineInfoPageResult = machineService.selectMachineInfoByPage(pageRequest);
            return Result.success(machineInfoPageResult);
        } catch (Exception e) {
            log.error("查询翻页信息错误",e);
            return  Result.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/addMachine",produces = "application/json")
    public String addMachine(MachineInfo machineInfo){
        log.info("接收到添加机台的请求，machineInfo",machineInfo);
        if(
                !StringUtils.hasLength(machineInfo.getMachineName())
                        || !StringUtils.hasLength(machineInfo.getMachineSN())
                        || machineInfo.getPosition()<0
                        || machineInfo.getTestTime() == null
                        || !StringUtils.hasLength(machineInfo.getPublish())
        ){
            return "参数校验失败，请检查入参";
        }

        Integer result = machineService.addMachine(machineInfo);
        if(result<=0){
            log.error("添加机台出错：machineInfo:{}",machineInfo);
            return "添加机台出错，请联系管理员";
        }
        return "";
    }

    @RequestMapping("/queryMachineInfoById")
    public MachineInfo queryMachineInfoById(Integer machineId){
        log.info("根据id查询机台，machine_info:" +machineId);
        MachineInfo machineInfo = null;
        try{
            machineInfo = machineService.queryMachineInfoById(machineId);
        } catch (Exception e) {
            log.error("查询机台失败",e);
        }
        return machineInfo;
    }

    @RequestMapping(value = "/updateMachine",produces = "application/json")
    public String updateMachine(MachineInfo machineInfo){
        log.info("接收到更新机台的请求",machineInfo);
        Integer result = machineService.updateMachine(machineInfo);
        if(result == 0){
            log.error("更新机台失败，请联系管理员");
            return "更新机台失败";
        }
        return "";
    }

    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接收批量删除机台的请求",ids);
        Integer result = machineService.batchDelete(ids);
        if(result < 0){
            log.error("批量删除失败",ids);
            return "批量删除失败";
        }
        return "";
    }





















}
